<?php
namespace frontend\controllers;

use common\jobs\NewTallyJob;
use common\jobs\NewUserJob;
use common\services\ToolService;
use Yii;

/**
 * Class IndexController
 * @package frontend\controllers
 * 首页展示控制器
 */
class IndexController extends BaseController
{
    /**
     * @var string
     */
    public $layout = 'prim';

    /**
     * init
     */
    public function init()
    {
        parent::init();
        $this->view->params['activeTab'] = 2;
        $this->view->params['seoData'] = [
            'title' => '首页',
            'keywords' => 'keywords',
            'description' => 'description',
        ];
    }

    /**
     * 判断登录状态
     * @return mixed
     */
    protected function checkLogin()
    {
        if(Yii::$app->user->isGuest){
            return $this->redirect(['login']);
        }
        return true;
    }

    /**
     * for test
     */
    public function actionTest()
    {
        if($_SERVER['APP_ENV'] !='dev') return ;
        /*Yii::$app->queue->push(new NewUserJob([
            'uid' => 1,
        ]));
        $a = new NewUserJob();
        $a->uid = 1;
        $a->initDefaultTally();
        $a->initDefaultAccount();*/
        /*$a = new NewTallyJob();
        $a->tallyId = 42;
        $a->uid = 1;
        $a->initDefaultStore();*/
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     * @throws
     */
    public function actionIndex()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 1;

        return $this->domainRender('index');
    }

    /**
     * @return string|\yii\web\Response
     * 登录页面
     */
    public function actionLogin()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 8;
        return $this->domainRender('login');
    }



    /**
     * @return string|\yii\web\Response
     * 登录页面
     */
    public function actionLoginvercode()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 8;
        return $this->domainRender('loginvercode');
    }

    /**
     * @return string|\yii\web\Response
     * 登录页面
     */
    public function actionForgetpass()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 8;
        return $this->render('forgetpass', [

        ]);
    }


    /**
     * @return string
     * 注册页面
     */
    public function actionRegister()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 9;
        return $this->domainRender('register');
    }

    /**
     * @return string
     * @desd 账本操作入口
     */
    public function actionTally()
    {
        $this->checkLogin();
        // 取出默认账本ID
        $tallyId = Yii::$app->request->get('id',0);
        if($tallyId == 'undefined') $tallyId = 0;
        // 默认取出本月数据
        $monthBeginDate  = date('Y-m-01', strtotime('-2 months'));
        $monthEndDate    = date('Y-m-01', strtotime("+1 month -1 day"));
        $this->view->params['activeTab'] = 3;
        return $this->render('tally', [
            //'monthBeginDate' => $monthBeginDate,
            //'monthEndDate'  => $monthEndDate,
            'tallyId' => $tallyId
        ]);
    }

    /**
     * @return string
     * @desc 收入流水列表
     */
    public function actionIncome()
    {
        $this->checkLogin();
        // 取出默认账本ID
        $tallyId = Yii::$app->request->get('id',0);
        // 默认取出本月数据
        $monthBeginDate  = date('Y-m-01', strtotime(date("Y-m-d")));
        $monthEndDate    = date('Y-m-d', strtotime("$monthBeginDate +1 month -1 day"));
        $this->view->params['activeTab'] = 4;
        return $this->render('record', [
            'monthBeginDate' => $monthBeginDate,
            'monthEndDate'  => $monthEndDate,
            'tallyId' => $tallyId,
            'recordType' => 10,
            'activeMenuIndex'=>'1-1'
        ]);
    }

    public function actionExpend()
    {
        $this->checkLogin();
        // 取出默认账本ID
        $tallyId = Yii::$app->request->get('id',0);
        // 默认取出本月数据
        $monthBeginDate  = date('Y-m-01', strtotime(date("Y-m-d")));
        $monthEndDate    = date('Y-m-d', strtotime("$monthBeginDate +1 month -1 day"));
        $this->view->params['activeTab'] = 5;
        return $this->render('record', [
            'monthBeginDate' => $monthBeginDate,
            'monthEndDate'  => $monthEndDate,
            'tallyId' => $tallyId,
            'recordType' => 20,
            'activeMenuIndex'=>'1-2'
        ]);
    }

    public function actionTransfer()
    {
        $this->checkLogin();
        // 取出默认账本ID
        $tallyId = Yii::$app->request->get('id',0);
        // 默认取出本月数据
        $monthBeginDate  = date('Y-m-01', strtotime(date("Y-m-d")));
        $monthEndDate    = date('Y-m-d', strtotime("$monthBeginDate +1 month -1 day"));
        $this->view->params['activeTab'] = 6;
        return $this->render('record', [
            'monthBeginDate' => $monthBeginDate,
            'monthEndDate'  => $monthEndDate,
            'tallyId' => $tallyId,
            'recordType' => 30,
            'activeMenuIndex'=>'1-3'
        ]);
    }

    /**
     * @return string
     * 关于我们页
     */
    public function actionAbout()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 5;
        return $this->domainRender('about');
    }

    /**
     * @return string
     * 产品介绍页
     */
    public function actionProduct()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 4;
        return $this->domainRender('product');
    }
    /**
     * @return string
     * 下载页面
     */
    public function actionDownload()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 6;
        return $this->domainRender('download');
    }

    /**
     * 个人中心页面
     */
    public function actionPersonal()
    {

        $this->view->params['activeTab'] = 10;
        return $this->render('personal', [

        ]);
    }

    /**
     * 账本列表页面
     */
    public function actionTallyList()
    {
        $this->checkLogin();
        $this->view->params['activeTab'] = 10;
        return $this->render('tallyList', [

        ]);
    }

    /**
     * 联系我们页面
     */
    public function actionContact()
    {
        if(Yii::$app->requestedRoute != 'index/login'){
            $this->redirect(array('/login'));
        }
        $this->view->params['activeTab'] = 11;
        return $this->render('contact', [

        ]);
    }
    /**
     * 代账专版
     */
    public function actionDz()
    {
        $this->view->params['activeTab'] = 88;
        return $this->render('dz', [

        ]);
    }

    /**
     * APP扫描下载页面
     */
    public function actionAppdown()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 99;
        return $this->domainRender('appdown');
    }

    /**
     * 帮助中心页面
     */
    public function actionHelp()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 12;
        return $this->domainRender('help');
    }

    /**
     * 隐私声明页面
     */
    public function actionPrivacystatement()
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        $this->view->params['activeTab'] = 89;
        return $this->domainRender('privacystatement');
    }

    /**
     * 隐私声明页面
     */
    public function actionUserText(): string
    {
        if($_SERVER['HTTP_HOST'] == 'juncheng.xingkongyun.com'){
            if(Yii::$app->requestedRoute != 'index/login' && Yii::$app->requestedRoute != 'index/forgetpass' && Yii::$app->requestedRoute != 'index/loginvercode'){
                $this->redirect(array('/login'));
            }
        }
        return $this->domainRender('user_text.php');
    }

    /**
     * 返回下载文件
     * @return mixed
     */
    public function actionDownExport()
    {
        $filename = Yii::$app->request->get('file','');
        $filepath = 'res/exports/'.$filename;
        if(!file_exists($filepath)){
            echo 'no data'; return false;
        }
        $ext = end(explode('.', $filename));
        $ext = strtolower($ext);
        if($ext == 'csv') {
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=\"".$filename."\"");
            $data = file_get_contents($filepath);
            echo $data;
            exit();
        }
    }

    /**
     * 隐私政策
     */
    public function actionPrivacy()
    {
        return $this->domainRender('privacy', [

        ]);
    }


    public function actionServiceText(){
        return $this->domainRender('service_text');
    }

    /**
     * 注册协议
     */
    public function actionRegProtocol()
    {
        return $this->domainRender('reg_protocol', [

        ]);
    }

    /**
     * 关联易公司财务管理系统
     * @param $financeId
     * @param $companyId
     * @param $companyName
     *
     * @return string
     */
    public function actionBindFinance($financeId,$companyId,$companyName)
    {
        $this->checkLogin();
        return $this->domainRender('bind-finance', [
            'financeId' => $financeId,
            'companyId' => $companyId,
            'companyName' => $companyName
        ]);
    }

    public function actionTestCate() {
        $baseStr = include_once Yii::$app->getBasePath().'/../common/config/baseCategoryConf.php';
        $baseCateConf =  json_decode($baseStr,true);
        print_r("<pre>");
        print_r($baseCateConf);
    }
}
